Yacon.data.BaseDataSet = function()
{
	 this.fID            = window.dataSetID;        
	 this.insertMode	 = "end"; 
	 this.attributes 	 = null;
};

Yacon.data.BaseDataSet.Extend(Yacon.util.LinkList,{
	
	setID:function(ID)
	{
		this.fID = ID;
	},
	getID:function()
	{
		return this.fID;
	},
	
	getState:function()
	{
		if (this.activeElement)
		{
			return this.activeElement.getState();
		}else
			return "past";
	},
	setState:function(state)
	{	
		if (this.activeElement)
		{
			return this.activeElement.setState(state);
		}
	},
	isEdit:function()
	{
		if (this.getState().isEqual("edit"))
			return true;
		else
			return false;
	},
	isInsert:function()
	{
		if (this.getState().isEqual("insert"))
			return true;
		else
			return false;
	},
	isDelete:function()
	{
		if (this.getState().isEqual("delete"))
			return true;
		else
			return false;
	},
	isPast:function()
	{
		if (this.getState().isEqual("past"))
			return true;
		else
			return false;
	},
	getRecord:function(record)
	{
		var curRecord = null;
		
		if (Yacon.isEmpty(record))
			curRecord = this.activeElement;
		
		else if (Yacon.isObject(record)&& Yacon.isString(record.ftype)&& record.ftype.isEqual("record"))
		{
			curRecord =  record;
			
		}else if (Yacon.isNumber(record))
		{
			if (record >-1 && record < this.getRecordCount())
			{
				var index = 0;
				var temp = this.firstElement;
				while (temp)
				{
					if (index == record)
					{
						curRecord = temp;
						break;
					}
					temp = temp.nextRecord();
					index ++;
				}
			}
		}
		this.activeElement = curRecord;
		return curRecord;
	},
	getRecordCount:function()
	{
		return this.length;
	},
	getFieldValue:function(fieldIndex)
	{
		if (!this.activeElement)
		{
			return "";
		}		
		return this.activeElement.getFieldValue(fieldIndex);
	},
	getFieldOldValue:function(fieldIndex)
	{
		if (!this.activeElement)
		{
			return "";
		}		
		return this.activeElement.getFieldOldValue(fieldIndex);
	},
	getFieldText:function(fieldIndex)
	{
		if (!this.activeElement)
		{
			return "";
		}		
		return this.activeElement.getFieldText(fieldIndex);
	},
	setPriFieldValue:function(fieldIndex,fieldValue)
	{
		if (this.activeElement)
		{
			this.activeElement.setFieldValue(fieldIndex,fieldValue);
			this.activeElement.setFieldText(fieldIndex,fieldValue);
		}	
	},
	setPriFieldValueText:function(fieldIndex,fieldValue,fieldText)
	{
		if (this.activeElement)
		{
			this.activeElement.setFieldValue(fieldIndex,fieldValue);
			this.activeElement.setFieldText(fieldIndex,fieldText);
		}		
	},
	createAttribute:function()
	{
		if (Yacon.isEmpty(this.attributes))
		{
			this.attributes = new Yacon.data.Record();	
		}		
	},
	addAttribute:function(attrName,attrValue)
	{
		this.createAttribute();
		this.attributes.setFieldValue(attrName,attrValue);
		
	},
	getAttribute:function(attrName)
	{
		if (Yacon.isEmpty(this.attributes))
		{
			return "";
		}
		return this.attributes.getFieldValue(attrName);
	},
	
	createPureRecord:function(state)
	{
		var newRecord = new Yacon.data.Record();	
		
		if (Yacon.isEmpty(state))
			state = "insert";
		
		newRecord.setState(state);
		if (this.activeElement){
			this.insert("after",this.activeElement,newRecord);
		}else{
			this.insert("begin",null,newRecord);
		}
		
		newRecord.setDataSet(this);
		return newRecord;	
	},
	
	createPriRecord:function(isrepeat)
	{
		this.createPureRecord();
		
		var prevEle = this.activeElement.prevElement;
		
		if(prevEle)
		{
			for (var i=0;i<prevEle.getFieldCount();i++)
			{
				if (isrepeat)
					this.activeElement.setFieldValue(prevEle.getField(i).getFieldName(),prevEle.getFieldValue(i));
				else
					this.activeElement.setFieldValue(prevEle.getField(i).getFieldName(),"");
			}
		}
		
		if (this.broadRecord)
			this.broadRecord("insert");
		
		return this.activeElement;	
	},
	editPriRecord:function(record)
	{
		this.getRecord(record);		
				
		if (this.activeElement && this.activeElement.getState()!="insert")
		{
			this.activeElement.setState("edit");
			
			if (this.broadRecord)
				this.broadRecord("edit");
		}
		return this.activeElement;
	},
	deletePriRecord:function(record)
	{
		
		this.getRecord(record);			
		if (this.activeElement)
		{
			if (this.activeElement.getState() == "insert")
			{
				this.cancelPriRecord(record);
			}
			else
			{
				this.activeElement.setState("delete");		
			}
		}
		return this.activeElement;
	},
	cancelPriRecord:function(record)
	{
		
		this.getRecord(record);			
		if (this.activeElement)
		{
			if (this.activeElement.getState() == "insert")
			{		
				this.activeElement.setState("delete");
				
				if (this.broadRecord)
					this.broadRecord("delete");
				
				this.deleteElement(this.activeElement);
				
				if (this.broadActiveRecord)
					this.broadActiveRecord();				
			}
			else
			{
				if (this.activeElement.getState() == "edit")
				{
					this.activeElement.fieldRevive();
					
					if (this.broadRecord)
						this.broadRecord("broad");		
				}		
				this.activeElement.setState("past");
			}
		}	
	}
});



Yacon.data.TitleDataSet = function()
{
	 /**
     * 数据集字段定义集合
     **/
    this.fieldListTitle			= new Yacon.data.FieldList();	    
    /**
     * 字段名称对象集合赋值数据集
     **/
    this.fieldListTitle.setDataSet(this);
};

Yacon.data.TitleDataSet.Extend(Yacon.data.BaseDataSet,{
	
	/**
	*创建字段对象
	*@fieldName  字段名称
	*@fieldValue 字段值
	*@return     标题字段对象
	**/
	createTitleField:function(fieldName,fieldLabel)
	{
	
		var fieldNameObj = this.fieldListTitle.getField(fieldName);
			
		/**首先判断此字段名称的字段是否存在，存在则不添加数据字段*/
		if (fieldNameObj == null)
		{
			fieldNameObj = new Yacon.data.FieldName();
			fieldNameObj.setFieldName(fieldName);
		
			this.fieldListTitle.addField(fieldNameObj);
			
			/**在每条记录添加数据字段**/
			var fieldValueObj  = new Yacon.data.FieldValue();
			fieldValueObj.setFieldName(fieldName);

			var recordElem = this.firstElement;	
			while(recordElem)
			{
				recordElem.addField(fieldValueObj);		
				recordElem = recordElem.nextElement;	
			}			
		}
		if (fieldLabel)	fieldNameObj.setFieldLabel(fieldLabel);
		
		return fieldNameObj;
	},
	/**
	*获取数据集字段个数
	*@return 字段个数
	**/
	getFieldCount:function()
	{
		return this.fieldListTitle.getFieldCount();
	},
	/**
	*获取标题字段对象
	*@fieldIndex   字段名称或字段索引
	*@return       字段对象或null
	**/
	getTitleField:function(fieldIndex)
	{
		if (this.fieldListTitle == null)
		{
			return null;
		}
		return this.fieldListTitle.getField(fieldIndex);
	},
	/**
	*获取字段标题
	*@fieldIndex   字段名称或字段索引
	*@return       字段标题或""
	**/
	getFieldLabel:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		
		if (field)
		{
			return field.getFieldLabel();
		}else{
			return "";
		}
	},
	/**
	*获取字段类型
	*@fieldIndex   字段名称或字段索引
	*@return       字段类型或"string"
	**/
	getFieldType:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		
		if (field)
		{
			return field.getFieldType();
		}else{
			return "string";
		}
	},
	/**
	*获取字段对齐
	*@fieldIndex   字段名称或字段索引
	*@return       对齐标示 left、center、right
	**/
	getFieldAlign:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		
		if (field)
		{
			return field.getFieldAlign();
		}else{
			return "left";
		}
	},
	
	/**
	*获取字段是否允许为空
	*@fieldIndex   字段名称或字段索引
	*@return       是否允许为空标示 false或true
	**/
	getFieldIsAllowNull:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldIsAllowNull();
		}else{
			return "true";
		}
	},
	/**
	*获取字段是否打开输入法
	*@fieldIndex   字段名称或字段索引
	*@return       是否打开输入法标示 false或true
	**/
	getFieldIsOpenMode:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldIsOpenMode();
		}else
		{
			return "true";
		}
	},

	/**
	*获取字段字符型最大长度
	*@fieldIndex   字段名称或字段索引
	*@return       字符型最大长度值 默认0
	**/
	getFieldMaxLength:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldMaxLength();
		}else
		{
			return 0;
		}
	},
	
	/**
	*获取字段小数点后最大长度
	*@fieldIndex   字段名称或字段索引
	*@return       小数点后最大长度 默认0
	**/
	getFieldDecimalLength:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldDecimalLength();
		}else
		{
			return 0;
		}
	},
	/**
	*获取字段整数最大长度
	*@fieldIndex   字段名称或字段索引
	*@return       整数最大长度 默认0
	**/
	getFieldIntLength:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldIntLength();
		}else
		{
			return 0;
		}
	},
	/**
	*获取字段最大值
	*@fieldIndex   字段名称或字段索引
	*@return       数值最大值 默认""
	**/
	getFieldMaxValue:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldMaxValue();
		}else
		{
			return "";
		}
	},
	/**
	*获取字段最小值
	*@fieldIndex   字段名称或字段索引
	*@return       数值最小值 默认""
	**/
	getFieldMinValue:function(fieldIndex)
	{
		var field = this.getTitleField(fieldIndex);
		if (field)
		{
			return field.getFieldMinValue();
		}else
		{
			return "";
		}
	}
	
});

Yacon.data.CustomDataSet = function()
{
	 this.whereRecord 	     = null;
	 this.foreignRecord      = null;
	 this.elementList        = null;	
	 this.tableList          = null;	
	
};

Yacon.data.CustomDataSet.Extend(Yacon.data.TitleDataSet,{
		
	/**
	*赋值字段值
	*@fieldIndex  字段名称或字段索引
	*@fieldValue  字段值
	**/
	setFieldValue:function(fieldIndex,fieldValue)
	{
		this.setPriFieldValue(fieldIndex,fieldValue);
		
		this.broadField(fieldIndex,fieldValue,fieldValue);
	},
	/**
	*赋值字段值
	*@fieldIndex  字段名称或字段索引
	*@fieldValue  字段值
	**/
	setFieldValueText:function(fieldIndex,fieldValue,fieldText)
	{
		this.setPriFieldValueText(fieldIndex,fieldValue,fieldText);
		
		this.broadField(fieldIndex,fieldValue,fieldText);
	},
	
	setForeignField:function(fieldName,fieldValue)
	{
		if (!this.foreignRecord)
		{
			this.foreignRecord = new Yacon.data.Record();	
		}
		this.foreignRecord.setFieldValue(fieldName,fieldValue);
		
	},
	getForeignField:function(fieldName)
	{
		if (!this.foreignRecord)
		{
			return "";
		}
		return this.foreignRecord.getFieldValue(fieldName);
	},
	/**
	 * 向后台获取数据条件记录
	 * @param fieldName 字段名称
	 * @param fieldValue 字段值
	 */
	setWhereField:function(fieldName,fieldValue)
	{
		if (!this.whereRecord)
		{
			this.whereRecord = new Yacon.data.Record();		
		}
	
		this.whereRecord.setFieldValue(fieldName,fieldValue);
		
	},
	getWhereField:function(fieldName)
	{
		if (!this.whereRecord)
		{
			return "";
		}
		return this.whereRecord.getFieldValue(fieldName);
	},
		
	/**
	 * 返回某字段、某值得记录数
	 * @param fieldIndex 字段名称或字段索引
	 * @param filedValue 字段值
	 * @return 总记录数
	 */
	getEqualFieldCount:function(fieldIndex,filedValue)
	{
		var fieldCount = 0;
		var fieldindex = 0;
		var record = this.firstElement;
		while (record)
		{
			if (record.getFieldValue(fieldIndex).isEqual(filedValue))
			{
				fieldCount ++;
			}		
			record = record.nextRecord();			
		}
		return fieldCount;
	},
	/**
	 * 返回某字段、某值得记录对象
	 * @param fieldIndex 字段名称或字段索引
	 * @param filedValue 字段值
	 * @return 返回null 或符合条件第一条记录
	 */
	getEqualRecord:function(fieldIndex,filedValue)
	{
		var equalRecord = null;
		
		var record = this.firstElement;
		while (record)
		{
			if (record.getFieldValue(fieldIndex).isEqual(filedValue))
			{
				equalRecord = record;
				break;
			}
			
			record = record.nextRecord();			
		}
		return equalRecord;
	},
	regElement:function(element)
	{
		if (!this.elementList)
			this.elementList = new Array();
		
		element.dataSet = this;
		this.elementList.add(element);	
	},
	unregElement:function(element)
	{
		if (this.elementList)
			this.elementList.remove(element);	
	},
	regTable:function(element)
	{
		if (!this.tableList)	
			this.tableList = new Array();	
					
		element.dataSet = this;
		this.tableList.add(element);
	},
	unregTable:function(element)
	{
		if (this.tableList)
			this.tableList.remove(element);	
	},
	broadRecord:function(type)
	{
		if (this.activeElement && this.tableList && type)
		{
			for(var i = this.tableList.size()-1;i>-1;i--)
			{
				try{
					if (type=="insert")
						this.tableList[i].appendRow();					
					if (type=="delete")
						this.tableList[i].deleteRow(this.activeElement);					
					if (type=="broad")
						this.tableList[i].broadRow();
					if (type=="edit")
						this.tableList[i].editRow();
					
				}catch(e)
				{
					this.tableList.remove(this.tableList[i]);
				}
			}	
		}		
	},
	broadActiveRecord:function()
	{
		if (this.activeElement && this.tableList)
		{
			for(var i = this.tableList.size()-1;i>-1;i--)
			{
				this.tableList[i].activeRowByRecord(this.activeElement);
			}	
		}		
	},
	broadField:function(fieldName,fieldValue,fieldText)
	{
		
		if (this.elementList)
		{
			for(var i = this.elementList.size()-1;i>-1;i--)
			{
				try{
					if (this.elementList[i].getFieldName().isEqual(fieldName))
					{
						this.elementList[i].setPriFieldValue(fieldValue,fieldText);
					}
				}catch(e)
				{
					this.elementList.remove(this.elementList[i]);
				}
			}
		}
		
		if (this.tableList)
		{
			for(var i = this.tableList.size()-1;i>-1;i--)
			{
				try{
					this.tableList[i].setPriFieldValue(fieldName,fieldValue,fieldText);
				}catch(e)
				{
					this.tableList.remove(this.tableList[i]);
				}
			}
		}
	},
	clearTableRow:function(element)
	{
		if (this.tableList)
		{
			for(var i = this.tableList.size()-1;i>-1;i--)
			{
				this.tableList[i].clearRow();
			}
		}
	},
	free:function()
	{
		this.fieldListTitle.free();
	   
		this.elementList    = null;
		this.tableList  = null;
		 
		if(this.foreignRecord)
			this.foreignRecord.free();
		
		if(this.whereRecord)
			this.whereRecord.free();
		
		if(this.attributes)
			this.attributes.free();	
		
		this.clear();
		
		this.clearTableRow();
	}

});












